<template>
  <div>
    <!-- 搜索 -->
    <!-- 索赔自动审核规则查询 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="90" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">下载</multiuse-button>
      </template>
    </FilterTable>
  </div>
</template>
<script>
//引入字典
import {dictionary} from '@/utils/dictMixin';
import {getListByPage, getListForExport,exporturl} from '@/api/claim/factory/ClaimAutoRuleQuery';
import {getPartType} from '@/api/dictionary/index';
import {exportExcelFile} from '@/utils/index';
import config from '@/config';

export default {
  mixins: [dictionary],
  data() {
    return {
      visible: false,
      collapse: false,
      fetchapi: getListByPage,
      topFilterInit: this.topFilterInitFun(),
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false
    };
  },
  created() {
    this.chuqi();
  },
  methods: {
    async chuqi(){
      // const res = await getPartType({ type: 5587 });
      // if (res.resultCode == 200) {
      //   this.topFilterInit.find(x => x.fieldName == 'opResult').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'opResult').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
      const res1 = await getPartType({ type: 5510 });
      if (res1.resultCode == 200) {
        let map = new Map()
        let cmsClaimTypeArr = []
        //有重复数据，去重
        res1.data.forEach(item=>{
          if (!map.has(item['codeId'])) {
            map.set(item['codeId'],item)
            cmsClaimTypeArr.push(item)
          }
        })
        // console.log(map)
        this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = cmsClaimTypeArr.map(y => ({ text: y.codeCnDesc, value: y.codeCnDesc }));
      }
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    save(obj) {
      this.topFilterInit.find(x => x.fieldName == 'model').initialValue = obj[0].address;
      this.visible = false;
    },
    // 导出
   async exportFileBtn(val) {
      //先不走下载中心的逻辑，目前走不通
      // let json = Object.assign({}, { CODE: 'CODE_00000111' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
     // this.fetchParams = Object.assign({}, this.fetchParams, val);
      getListForExport(this.fetchParams)
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '系统规则',
          fieldName: 'sysRule',
          placeholder: '请输入系统规则...',
          initialValue: ''
        },
        // {
        //   type: 'SELECT',
        //   label: '操作结果',
        //   fieldName: 'opResult',
        //   initialValue: '',
        //   filterable: true,
        //   placeholder: '请选择操作结果...',
        //   itemList:[]
        // },
        {
          type: 'SELECT',
          label: '适用类型',
          initialValue: '',
          fieldName: 'cmsClaimType',
          filterable: true,
          placeholder: '请选择适用类型...',
          itemList: []
        }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '规则编号',
          dataIndex: 'ruleNo',
          sorter: true
        },
        {
          title: '系统规则',
          dataIndex: 'sysRule',
          sorter: true
        },
        // {
        //   title: '系统操作结果',
        //   dataIndex: 'opResultStr',
        //   sorter: true
        // },
        {
          title: '适用索赔类型',
          dataIndex: 'cmsClaimType',
          sorter: true
        },
        {
          title: '备注',
          dataIndex: 'remarks',
          sorter: true
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
